//
//  BrowserCollectionViewCell.swift
//  weibo1
//
//  Created by changhongliang on 2017/8/3.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

//Swift中定义代理协议需要继承于NSObjectProtocol
//并且默认情况下所有的代理方法都是必须实现的
protocol BrowserCollectionViewCellDelegate:NSObjectProtocol
{
    func broserCollectionViewCellDidClick(cell:BrowserCollectionViewCell)
}

class BrowserCollectionViewCell: UICollectionViewCell {
    
    ///代理
    //注意:代理属性前面一定要写weak
    weak var delegate:BrowserCollectionViewCellDelegate?
    
    var url:URL? {
        didSet{
            //重置,防止循环引用出现的问题
            reset()
            
            iconImageView.sd_setImage(with: url) { (image, error, _, _) in
                
                guard let imagesize = image?.size else {
                    return
                }
                //1.按照宽高比缩放图片
                let scale = imagesize.height / imagesize.width
                let height = scale * ScreenWidth
                self.iconImageView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: ScreenWidth, height: height))
                
                //2.判断是长图还是短图
                if height < ScreenHeight {
                    //短图,需要居中
                    //1.1计算偏移位
                    let offsetY = (ScreenHeight - height) * 0.5
                    //1.2设置偏移位
                    self.scrollview.contentInset = UIEdgeInsets(top: offsetY, left: 0, bottom: offsetY, right: 0)
                }else{
                    //长图不需要居中
                    self.scrollview.contentSize = self.iconImageView.frame.size
                
                }
               
            }
        
        
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        //1.添加子控件
        contentView.addSubview(scrollview)
        scrollview.addSubview(iconImageView)
        //2.布局子控件
        scrollview.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }

        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 重置所有属性
    private func reset()
    {
        scrollview.contentSize = CGSize.zero
        scrollview.contentOffset = CGPoint.zero
        scrollview.contentInset = UIEdgeInsets.zero
        iconImageView.transform = CGAffineTransform.identity
    }
    

    
    // MARK: -懒加载
    private lazy var scrollview :UIScrollView = {
    let sl = UIScrollView()
        //和缩放相关的设置
        sl.minimumZoomScale = 0.5
        sl.maximumZoomScale = 10.0
        sl.delegate = self
        return sl
    }()
    
     lazy var iconImageView:UIImageView = {
        let image = UIImageView()
        image.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(BrowserCollectionViewCell.imageClick))
        image.addGestureRecognizer(tap)
        return image
    }()
    
    func imageClick() {
      delegate?.broserCollectionViewCellDidClick(cell: self)
    }
    
    
}

extension BrowserCollectionViewCell:UIScrollViewDelegate
{
    //告诉谁需要缩放
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return iconImageView
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        //调整图片位置,让图片居中
        //scrollview被缩放的view,它的frame和bounds是有一定的区别的
        //bounds 的值是固定的,而frame的值是不断变化的
        //所以说被缩放的控件的frame 就是scrollview的contensize
        //也就是说frame的值和contentsize一样的
        let offsetY = (ScreenHeight - iconImageView.frame.height) * 0.5
        let offsetX = (ScreenWidth - iconImageView.frame.width) * 0.5
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX)
    }

}
